var App = getApp();
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';

//Page Object
Page({
    data: {
        addrList: [],
        isNoMore: false,
        page: {
            page: 1,
            pageSize: 10
        },
    },
    //options(Object)
    onLoad: function (options) {
        this.getData()
    },

    getData: function () {
        
        const _this = this
        let params = Object.assign({}, _this.data.page)
        App.globalData.userModel.addrList({
            data: params
        }).then(json => {
            _this.setData({
                addrList: json.data
            })
            // if (_this.data.page.page == 1) {
            //     _this.setData({
            //         addrList: json.data.list,
            //         'page.page': ++_this.data.page.page
            //     })
            // } else {
            //     _this.setData({
            //         'page.page': ++_this.data.page.page,
            //         addrList: _this.data.addrList.concat(json.data.list)
            //     })
            // }
            // if (json.data.list.length < _this.data.page.pageSize) {
            //     _this.setData({
            //         isNoMore: true
            //     })
            // }
        })
    },

    onReachBottom: function () {
        this.getData()
    },
    deleteAddr: function (res) {
        const _this = this
        Dialog.confirm({
            title: '确认删除',
            message: '删除地址？'
        }).then(() => {
            App.globalData.userModel.deleteAddr({
                data: {
                    id: res.currentTarget.dataset.id
                }
            }).then(json => {
                var addrList = _this.data.addrList.filter((item => {
                    return item.id != res.currentTarget.dataset.id
                }))
                _this.setData({
                    addrList: addrList
                })
            })
        }).catch(() => {
            // on cancel
        });
    },
    goAddrInfo: function (res) {
        wx.navigateTo({
            url: '/pages/person/location/info/info?item=' + JSON.stringify(res.currentTarget.dataset.item)
        });
    }
});